﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GzipManagerPro.IO;
using GzipManagerPro.UI.Utility;
using System.IO;
using GzipManagerPro.UI.Dialogs;

namespace GzipManagerPro.UI.Controls.Preview
{
    public partial class ArchivePreview : PreviewBase
    {
        private Archive Archive;

        public ArchivePreview()
        {
            InitializeComponent();
        }

        public override void GeneratePreview(string path)
        {
            base.GeneratePreview(path);

            try
            {
                Archive = Archive.FromFile(path);
                ArchiveExplorerProvider provider = new ArchiveExplorerProvider() { Archive = Archive };
                this.Explorer.Provider = provider;
                this.Explorer.DoubleClick += Explorer_DoubleClick;
                if (string.IsNullOrEmpty(Archive.StoryBoard))
                    this.SplitContainer.Panel2Collapsed = true;
                else
                {
                    try
                    {
                        this.StoryBoard.Rtf = Archive.StoryBoard;
                    }
                    catch
                    {
                        this.StoryBoard.Text = Archive.StoryBoard;
                    }
                }
            }
            catch (Exception ex)
            {
                ex.MessageBox();
            }
        }

        private void Explorer_DoubleClick(object sender, EventArgs e)
        {
            this.OpenCommand(null);
        }

        public override Command[] GetCommands(CustomFileSystemInfo info)
        {
            List<Command> commands = new List<Command>();

            Image image = GzipManagerPro.UI.Properties.Resources.ExtractAll;
            commands.Add(new Command()
            {
                Title = "Extract All",
                Method = o => Utility.Helper.ExtractTo(Path.GetDirectoryName(o.FullName), this.Archive.FileName, null),
                Image = image
            });

            commands.Add(new Command()
            {
                Title = "Extract All To...",
                Image = image,
                Method = o =>
                {
                    FolderBrowserDialog dialog = new FolderBrowserDialog()
                    {
                        Description = "Choose a folder to extract archive:",
                        SelectedPath = Path.GetDirectoryName(o.FullName)
                    };

                    if (dialog.ShowDialog() == DialogResult.Cancel)
                        return false;

                    return Utility.Helper.ExtractTo(dialog.SelectedPath, this.Archive.FileName, null);
                }
            });

            commands.Add(new CommandSeparator());

            commands.Add(new Command()
            {
                Title = "Extract To...",
                Image = image,
                Method = o =>
                {
                    if (this.Explorer.SelectedItem == null)
                    {
                        "Choose an item from th list and try again.".Warning("Open...");
                        return false;
                    }

                    FolderBrowserDialog dialog = new FolderBrowserDialog()
                    {
                        Description = "Choose a folder to extract item:",
                        SelectedPath = Path.GetDirectoryName(o.FullName)
                    };

                    if (dialog.ShowDialog() == DialogResult.Cancel)
                        return false;

                    return Utility.Helper.ExtractTo(dialog.SelectedPath, (this.Explorer.SelectedItem.RowObject as ArchiveFileSystemInfo).Info, ref this.Password);
                }
            });

            commands.Add(new CommandSeparator());

            commands.Add(new Command()
            {
                Title = "Open...",
                Method = o => this.OpenCommand(o),
                Image = GzipManagerPro.UI.Properties.Resources.Open
            });
            return commands.ToArray();
        }

        private bool OpenCommand(CustomFileSystemInfo info)
        {
            if (this.Explorer.SelectedItem == null)
            {
                "Choose an item from th list and try again.".Warning("Open...");
                return false;
            }

            ArchiveItem i = (this.Explorer.SelectedItem.RowObject as ArchiveFileSystemInfo).Info;

            if (i is Folder)
            {
                "Choose a file from th list and try again.".Warning("Open...");
                return false;
            }

            return Utility.Helper.Open(this.Archive, i);
        }

        private string Password;
    }
}
